/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.entity.SysDictEntity;
import io.renren.modules.sys.entity.SysDictItemEntity;
import io.renren.modules.sys.entity.SysOrgEntity;
import io.renren.modules.sys.service.SysDictItemService;
import io.renren.modules.sys.service.SysDictService;
import io.renren.modules.sys.service.SysEmployeeService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统字典项
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/dictItem")
public class SysDictItemController extends AbstractController {
	@Autowired
	private SysDictItemService sysDictItemService;
	@Autowired
	private SysDictService sysDictService;


	/**
	 * 所有字典项列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:dictItem:list")
	public R list(@RequestParam Map<String, Object> params){
		String dictCode = (String)params.get("dictCode");
		//String dictItemCode = (String)params.get("dictItemCode");
		//String dictItemName = (String)params.get("dictItemName");
		List list = sysDictItemService.queryByDictItemNameOrCode(dictCode);
		PageUtils page = new PageUtils(list, list.size(), 100, 1);
//		PageUtils page = sysDictItemService.queryPage(params);
//		List list = page.getList();
//		for(int i=0;i<list.size();i++){
//			SysDictItemEntity dictItem = (SysDictItemEntity)list.get(i);
//			QueryWrapper qw = new QueryWrapper<SysDictEntity>()
//					.like(StringUtils.isNotBlank(dictItem.getDictCode()),"dict_code", dictItem.getDictCode());
//			dictItem.setDictName(sysDictService.getOne(qw).getDictName());
//		}
		return R.ok().put("page", page);
	}

	/**
	 * 字典项信息
	 */
	@GetMapping("/info/{id}")
	@RequiresPermissions("sys:dictItem:info")
	public R info(@PathVariable("id") Long id){
		SysDictItemEntity dictItem = sysDictItemService.getById(id);
		return R.ok().put("dictItem", dictItem);
	}
	
	/**
	 * 保存字典项
	 */
	@SysLog("保存字典项")
	@PostMapping("/save")
	@RequiresPermissions("sys:dictItem:save")
	public R save(@RequestBody SysDictItemEntity dictItem){
		ValidatorUtils.validateEntity(dictItem, AddGroup.class);
		sysDictItemService.saveDictItem(dictItem);
		
		return R.ok();
	}
	
	/**
	 * 修改字典项
	 */
	@SysLog("修改字典项")
	@PostMapping("/update")
	@RequiresPermissions("sys:dictItem:update")
	public R update(@RequestBody SysDictItemEntity org){
		ValidatorUtils.validateEntity(org, UpdateGroup.class);
		sysDictItemService.update(org);
		
		return R.ok();
	}
	
	/**
	 * 删除字典项
	 */
	@SysLog("删除字典项")
	@PostMapping("/delete")
	@RequiresPermissions("sys:dictItem:delete")
	public R delete(@RequestBody Long[] dictItemIds){
		sysDictItemService.deleteBatch(dictItemIds);
		
		return R.ok();
	}
}
